1810G - The Maximum Prefix - CodeForces Solution


dp *3200

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int N=5010;
int T,n,x,y;
int a[N],b[N],dp[N][N];
int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
int dec(int x,int y){return x<y?x-y+mod:x-y;}
int mul(int x,int y){return 1ll*x*y%mod;}
int ksm(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=mul(x,x)) if (y&1) ans=mul(ans,x);
	return ans;
}
int main(){
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);
		for (int i=1;i<=n;i++){
			scanf("%d%d",&x,&y);
			a[i]=mul(x,ksm(y,mod-2));
			b[i]=(1-a[i]+mod)%mod;
		}
		for (int i=0;i<=n;i++) scanf("%d",&dp[1][i]);
		for (int i=2;i<=n+1;i++)
			for (int j=0;j<=n;j++) dp[i][j]=0;
		for (int i=1;i<=n;i++){
			for (int j=1;j<=n;j++) dp[i+1][j-1]=add(dp[i+1][j-1],mul(dp[i][j],a[i]));
			for (int j=0;j<n;j++) dp[i+1][j+1]=add(dp[i+1][j+1],mul(dp[i][j],b[i]));
			dp[i+1][0]=add(dp[i+1][0],mul(dp[i][0],b[i]));
		}
		for (int i=2;i<=n+1;i++) printf("%d ",dp[i][0]);
		putchar('\n');
	}
}


Comments

Submit
0 Comments
More Questions

1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel